<template>
  <div class="app-container">
    <div class="btn_content">
      <el-button @click="addClassTeaching" type="primary">创建授课班级</el-button>
    </div>
    <div class="search_content">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="班级名称" prop="classTeachingName">
          <el-input
            v-model="queryParams.classTeachingName"
            placeholder="请输入班级名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="创建时间" prop="date">
          <el-date-picker
            value-format="yyyy-MM-dd HH:mm:ss"
            v-model="queryParams.date"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            v-preventReClick
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
          >搜索
          </el-button>
          <el-button
            v-preventReClick
            icon="el-icon-refresh"
            size="mini"
            @click="resetQuery"
          >重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div style="height: 30px">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </div>
    <div class="table_content">
      <el-table
        height="calc(100vh - 340px)"
        v-loading="loading"
        :data="classTeachingList"
      >
        <el-table-column label="班级名称" align="center" prop="classTeachingName"/>
        <el-table-column label="班级人数（人）" align="center" prop="stuNum"/>
        <el-table-column label="在学课程（门）" align="center" prop="studyingCourseNum"></el-table-column>
        <el-table-column label="已学课程（门）" align="center" prop="studiedCourseNum"></el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime">
          <template slot-scope="scope">
            {{ parseTime(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="180"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              v-preventReClick
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleDetail(scope.row)"
            >
              详情
            </el-button>
            <el-button
              v-preventReClick
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
            >
              编辑
            </el-button>
            <el-button
              v-preventReClick
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <classTeachingDialog :title="title" :showCreateClass.sync="showCreateClass" :classTeachingId="classTeachingId" @refresh="getList"
    ></classTeachingDialog>
  </div>
</template>

<script>
import { getClassTeachingList, deleteSchoolClassTeaching } from '@/api/teach/teach'
import classTeachingDialog from './components/classTeachingDialog'

export default {
  name: 'SchoolClassTeaching',
  components: {
    classTeachingDialog
  },
  data() {
    return {
      classTeachingId: '',
      title: '创建授课班级',
      formLoading: false,
      showCreateClass: false,
      treeData: [],
      form: {
        classTeachingName: '',
        studentIdList: []
      },
      rules: {
        classTeachingName: [
          { required: true, message: '请输入授课班级名称', trigger: 'blur' }
        ],
        studentIdList: [
          { required: true, message: '请选择学生', trigger: 'change' }
        ]
      },
      loading: false,
      // 授课班级列表
      classTeachingList: [],
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        classTeachingName: '',
        pageNum: 1,
        pageSize: 10,
        date: []
      },
      // 显示搜索条件
      showSearch: true
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleDetail(row) {
      this.$router.push({ path: '/schoolOrganization/classTeachingDetial', query: { classTeachingId: row.classTeachingId } })
    },
    addClassTeaching() {
      this.title = '创建授课班级'
      this.showCreateClass = true
    },
    async handleEdit(row) {
      this.classTeachingId = row.classTeachingId
      this.title = '编辑授课班级'
      this.showCreateClass = true
    },
    async handleDelete(row) {
      this.$confirm(`确认删除【${row.classTeachingName}】授课班级?删除后不可恢复，请谨慎处理！`, '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const loading = this.$loading({
          lock: true,
          text: '正在删除中',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.3)'
        })
        try {
          let res = await deleteSchoolClassTeaching(row.classTeachingId)
          const { code } = res
          if (code == 200) {
            this.$modal.msgSuccess('删除成功')
            this.getList()
          }
        } catch (e) {
          console.error(e)
        } finally {
          loading.close()
        }
      }).catch(() => {
        this.$modal.msgInfo('已取消')
      })
    },
    async getList() {
      try {
        this.loading = true
        let params = {
          ...this.queryParams
        }
        if (this.queryParams.date && this.queryParams.date.length > 0) {
          params.beginCreateDate = this.queryParams.date[0]
          params.endCreateDate = this.queryParams.date[1]
        }
        delete params.date
        let res = await getClassTeachingList(params)
        const { code, rows } = res
        if (code == 200) {
          this.classTeachingList = rows
          this.total = res.total
        }
      } catch (e) {
        console.error(e)
      } finally {
        this.loading = false
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>

<style scoped lang="scss">
.app-container {
  .btn_content {
    margin-bottom: 20px;
  }

  .table_content {
    margin-top: 10px;
  }
}
</style>
